package org.example.commons;

import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalException {

    @ExceptionHandler(Exception.class)
    public ApiResponse<Void> exception(Exception e) {
//        log.error("异常信息", e);
        return ApiResponse.error();
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ApiResponse<Void> exception(MethodArgumentNotValidException e) {
        FieldError fieldError = e.getBindingResult().getFieldError();
        log.error("异常信息", e);
        return ApiResponse.error(fieldError.getDefaultMessage());
    }
}
